package com.hhit.collaborative_learning.communication;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.CoopDao;
import com.hhit.collaborative_learning.dao.TeamDao;
import com.hhit.collaborative_learning.dao.TeamUserDao;
import com.hhit.collaborative_learning.dao.UserDao;
import com.hhit.collaborative_learning.dao.impl.CoopDaoImpl;
import com.hhit.collaborative_learning.dao.impl.TeamDaoImpl;
import com.hhit.collaborative_learning.dao.impl.TeamUserDaoImpl;
import com.hhit.collaborative_learning.dao.impl.UserDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.DateUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.hhit.collaborative_learning.utils.StringUtil;
import com.mysql.jdbc.StringUtils;

/**
 * Servlet implementation class PutCoopProjServlet
 */
@WebServlet("/communication/PutCoopProjServlet")
public class PutCoopProjServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PutCoopProjServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] userName=request.getParameterValues("userName[]");
		String[] userDesc=request.getParameterValues("userDesc[]");
		String projTitle=request.getParameter("projTitle");
		String projDesc=request.getParameter("projDesc");
		String user_id=request.getParameter("user_id");
		Map<String, Integer> uid_name=new HashMap<String, Integer>();
		Map<String, String> udesc_name=DateUtil.getMapFromStringArray(userName, userDesc);
		Map<Integer,String> resultMap= new HashMap<Integer, String>();
		String uname=StringUtil.formatArraytoString(0, userName, ","),isSuccess="false";
		int teamID=0,rtu=0,rcp=0;
		List<Integer> uId=new ArrayList<Integer>();
		JSONObject jobj = new JSONObject();
		Connection con=DBAccess.getInstance().getConn();
		UserDao ud=new UserDaoImpl();
		TeamDao dt=new TeamDaoImpl();
		TeamUserDao tud=new TeamUserDaoImpl();
		CoopDao cd=new CoopDaoImpl();
		if(!StringUtils.isNullOrEmpty(uname)){
			uname=" u.userNickName in ("+uname+")";
		}
		ResultSet reUid= ud.queryUserId(con, 0, 0, uname, null, null);
		try {
			while (reUid.next()){
				uid_name.put(reUid.getString(2), reUid.getInt(1));
			}
			if(uid_name.size()!=udesc_name.size()){
				System.out.println("添加的某些用户不存在");
			}else{
				teamID=dt.insert(con, projTitle+"的小队", user_id, "致力于"+projTitle);
				if(teamID==0){
					System.out.println("添加小组失败");
				}else{
					resultMap=DateUtil.getMapFromMap(uid_name, udesc_name);
					rtu=tud.insertArray(con, resultMap, ""+teamID);
					rcp=cd.insert(con, projTitle, ""+teamID, "/images/common_23_icon.png", user_id, projDesc);
					System.out.println("添加小组"+teamID+"添加成员："+rtu+"  添加项目："+rcp);
				}
			}
			if(teamID*rtu*rcp!=0){
				isSuccess="true";
			}
			jobj.put("isSuccess", isSuccess);
			ResponseUtil.write(response, jobj.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBAccess.getInstance().close(con, null, null);
			System.out.println("coop_project  "+jobj.toString());
		}
	}

}
